#Replication Code for 
#Digital intermediaries, market competition, and citizen-state interactions
#Authors- Anustubh Agnihotri
#The Files replicates the results showing transition from Offline to Online Appplications

rm(list = ls())

pks <- c("tidyverse", "haven", "lfe", "stargazer", 
         "xtable", "kableExtra","ggplot2","dplyr","lubridate","zoo",
         "astsa","panelView")
lapply(pks, function(x) if (!require(x, character.only=T)){library(x, character.only = T)})

##Enter the local directory where the files Offline_Data.RData/Online_Data.RData
##are stored

dir=''


##Load the Offline and Online data 
##The data has been aggregated and deannonymized 

load(paste0(dir,'Offline_Data.RData'))
load(paste0(dir,'Online_Data.RData'))


##Create variable that indicates transition from Offline to Online 
offline_data_qtr_overall$Online=1
online_data_qtr_overall$Online=0

##Merge the two datasets
data_overall<-rbind(offline_data_qtr_overall,online_data_qtr_overall)


##Plot Figure 11 Part 1
ggplot(data = data_overall, aes(x = Year_Qtr, y = time))+
  geom_point(data = filter(data_overall, Online==0), size=2, linetype = "solid")+ 
  geom_errorbar(aes(ymin=time-sd, ymax=time+sd),
                width=.01, size=0.55, colour="black") +
  scale_x_yearqtr() + 
  geom_point(data = filter(data_overall, Online==1), aes(color="blue"),size=3, linetype = "dashed") + 
  geom_errorbar(aes(ymin=time-sd, ymax=time+sd),
                width=.01, size=0.55, colour="black") +
  geom_vline(xintercept = as.yearqtr('2014 07',"%Y %m"),color = "#FC4E07",size=2)+ 
  geom_hline(yintercept=30, linetype="solid", color = "red") +  
  ylab('Processing Time All Caste Certificates') +
  theme(legend.position="none") + ggtitle('Paper to Online Transition')

##Plot Figure 11 Part 2
ggplot(data = data_overall, aes(x = Year_Qtr, y = Applications))+
  geom_line(data = filter(data_overall, Online==0), size=1, linetype = "solid")+ 
  scale_x_yearqtr() + 
  geom_line(data = filter(data_overall, Online==1), aes(color="blue"),size=1, linetype = "dashed") + 
  geom_vline(xintercept = as.yearqtr('2014 07',"%Y %m"),color = "#FC4E07",size=1)+  
  ylab('Applications  All Caste Certificates') +
  theme(legend.position="none") + ggtitle('Paper to Online Transition')